home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group99a.txt
/
000234_icon-group-sender _Thu Nov 18 08:17:23 1999.msg
< prev
next >
Wrap
Internet Message Format
|
2000-09-20
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id IAA26980
for icon-group-addresses; Thu, 18 Nov 1999 08:17:13 -0700 (MST)
Message-Id: <199911181517.IAA26980@baskerville.CS.Arizona.EDU>
From: swampler@noao.edu (Steve Wampler)
Date: Wed, 17 Nov 1999 18:03:59 MST
To: Kostas Oikonomou <oikonomou@att.com>
Subject: Re: Strange table behavior
Cc: icon-group@optima.CS.Arizona.EDU
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
On Nov 17 at 5:48pm, Kostas Oikonomou writes:
} When I run this program
}
} procedure main()
} local T
} T := table([])
} put(T[1], "A")
} write("*T = ", *T)
} write(T[1][1])
} end
}
} I get
}
} *T = 0
} A
}
} Can anyone explain to me what's happening here? Does it have to do with
} T being a table of lists?
I can explain it, but I can't decide if it should be classified as a bug
or a "feature"...(right now I'm leaning toward "feature")
First:
T := table([])
This creates a table with a specific list as the default value (note,
you probably didn't mean this, see below). Let's call this particular
list L_001, so we can see what happens later.
If you were to ask for the size of the table at this time, you'd
expect to see the value 0, because you haven't assigned anything to
the table yet.
Now:
put(T[1], "A")
This asks for the value of T[1]. Since you haven't assigned anything
to the table yet, you get back the default value, which is L_001 in
this case. The string "A" is put into the list L_001, increasing the
size of this list by 1. *Note that you have not yet assigned anything
to the table!*
So:
write("*T = ", *T)
outputs the size of the table. Since nothing has been assigned yet,
it's still 0.
Finally:
write(T[1][1])
this first gets the value of the element T[1]. Since nothing has been
assigned to the table yet, you get the default value, good old list
L_001. Then the first element of L_001 is printed, producing the "A".
The output is correct, the size of the table has never changed, because
nothing has ever been assigned to the table! The fact that something
was assigned to the list that was created and used as the default value
for the table doesn't change this fact.
Ok, now - why isn't T := table([]) what you want. Again, breaking it
down:
[] creates a list (L_001, say).
table(L_001) says to create a table and make list L_001 the default
value. Note that it doesn't say "create a new list each time a
new table element is needed". It says "use the list (L_001)" whenever
you need the default value". So, for example:
put(T[1], "A")
put(T[2], "B")
put(T[3], "C")
write(T[4][2])
would output "B" and
write(*(T["aslkdjfsa"])
would output "3"! (It's the same list being produced as the
default value, regardless of the reference key.)
What you want to do is something like:
T := table()
/T[1] := [] # create a brand new list and assign it only to T[1]
# if nothing has ever been assigned to T[1].
put(T[1], "A") # put the A into this brand new list
I hope this helps!
--
--
Steve Wampler- SOLIS Project, National Solar Observatory
swampler@noao.edu